from django.shortcuts import render
from django.views import View
from animal_center.apps.animal_index.models import Animalinfo

from django.http import HttpResponse
from django.template import loader, Context
# Create your views here.

class DetailView(View):

    def get(self,request, animalid):
        animal = Animalinfo.objects.get(id=animalid)
        # 构造上下文
        dd = {
            'animal_name': animal.animal_name,
            'animal_detail': animal.animal_detail,
            'animal_env': animal.animal_env,
            'animal_facility': animal.animal_facility,
            'animal_bio': animal.animal_bio,
            'animal_supplier': animal.animal_supplier,
            'animal_contact':animal.animal_contact,
            'animal_usedep':animal.animal_usedep,
            'animal_num':animal.animal_num
        }


        return render(request, 'detail.html', dd)


class Xls_dl(View):
    def get(self,request, animalid,filename):
        animal = Animalinfo.objects.get(id=animalid)
        # 构造上下文
        dd = {
            '动物名称': animal.animal_name,
            '动物品系': animal.animal_detail,
            '动物饲养环境': animal.animal_env,
            '动物饲养设施': animal.animal_facility,
            '动物生物学信息': animal.animal_bio,
            '动物供应商': animal.animal_supplier,
            '供应商联系方式':animal.animal_contact,
            '动物使用单位':animal.animal_usedep,
            '库存数量':animal.animal_num
        }
        response = HttpResponse(content_type='application/ms-excel')
        response['Content-Disposition'] = 'attachment; filename=%s.xls' % filename

        t = loader.get_template('xls.html')
        c = {
            'data': dd,
        }
        response.write(t.render(c))
        return response